<?php

    require 'functions.php';
       
    $db = DBControl::getInstance();
       
    if (!is_numeric($_POST['friend_id']))
    {
        die(json_encode(array('error' => 'no friend id')));
    }
    else
    {
        $friend_id = $_POST['friend_id'];
    }
    
    if (DEBUG==true)
    {
        $output['post'] = json_encode($_POST);
    }
    
    //genderage handling
    $output['additionalInfo']['sex'] = $_POST['sex'];
    if (is_numeric($_POST['age']) && ($_POST['sex']=='M' || $_POST['sex']=='K'))
    {
        $age = $_POST['age'];
        if ($age > DB_MAX_AGE)
        {
            $age = DB_MAX_AGE-1;
        }
        if ($age < DB_MIN_AGE)
        {
            $age = DB_MIN_AGE+1;
        }
        $gender = $_POST['sex'];
        $values = array(
            $gender,
            $age,
            LIKED_PRODUCT_COUNT
        );

        $result = $db->PDOquery('SELECT item.id, title, mediumimagelink, amazonurl FROM item JOIN genderagerecomendation ON item.id=genderagerecomendation.item_id WHERE gender = ? AND agegroup_id = (SELECT id FROM agegroup WHERE ? BETWEEN minage AND maxage) ORDER BY ordernumber LIMIT ?', $values);
        if (!empty($result))
        {
            $output['genderage'] = $result;
        }
        else
        {
            $output['genderage'] = NULL;
        }

        $output['additionalInfo']['age'] = $age;
    }
    else
    {
        $output['genderage'] = NULL;
    }

    //handle likes
    $user_weights = array_fill(0, VECTOR_LENGTH, 0);
    $sum = 0;
    for ($i = 0; $i < VECTOR_LENGTH; $i++)
    {
        $identifier = 'category_' . $i;
        if (isset($_POST[$identifier]))
        {
            $user_weights[$i] = 1;
            $sum += 1;
        }
    }
    if ($sum > 0)
    {
        //normalization
        foreach ($user_weights as &$value) 
        {
            $value = $value/$sum;
        }
        $tmpWeights = $user_weights;
        array_push($tmpWeights, LIKED_PRODUCT_COUNT);
        //$weight_likes_query from functions.php
        $result = $db ->PDOquery($weight_likes_query, $tmpWeights);
        
        if (!empty($result))
        {
            $output['likes'] = $result;
        }
        else
        {
            $output['likes'] = NULL;
        }
    }
    else
    {
        $output['likes'] = NULL;
    }

    //gender handling
    //always null
    $output['gender'] = NULL;
    
    $user_info = $db ->PDOquery("SELECT id FROM users WHERE facebook_id = ? LIMIT 1;", array($friend_id));
    if (empty($user_info))
    {
        //add user info
        $insert_data = array(
            (isset($age)) ? $age : null,
            $_POST['sex'],
            ($sum > 0) ? weightsArrayToString($user_weights) : null,
            $friend_id
        );

        $user_id = $db -> PDOquery('INSERT INTO users (age, gender, weights, facebook_id) VALUES (?,?,?,?) RETURNING id;', $insert_data);
        $user_id = $user_id[0]['id'];
    }
    else
    {
        $user_id = $user_info[0]['id'];
    }
   
    $output['recommendation_id'] = $user_id;
    
    echo json_encode($output);

?>
